<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品ID" prop="productId">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入商品ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品状态(1正常,0下架)" prop="productStatus">
        <el-select v-model="queryParams.productStatus" placeholder="请选择商品状态(1正常,0下架)" clearable>
          <el-option
            v-for="dict in pro_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['product:product:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Sort"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="productList"
      row-key="categoryId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="商品名称" prop="productName" />
      <el-table-column label="商品状态(1正常,0下架)" align="center" prop="productStatus">
        <template #default="scope">
          <dict-tag :options="pro_status" :value="scope.row.productStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="商品图片" align="center" prop="productImage" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.productImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="商品排序" align="center" prop="productSort" />
      <el-table-column label="销售价格" align="center" prop="salePrice" />
      <el-table-column label="进货价格" align="center" prop="purchasePrice" />
      <el-table-column label="商品介绍" align="center" prop="productDesc" />
      <el-table-column label="库存量" align="center" prop="stock" />
      <el-table-column label="销量" align="center" prop="salesVolume" />
      <el-table-column label="商品类目ID" align="center" prop="categoryId" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['product:product:edit']">修改</el-button>
          <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)" v-hasPermi="['product:product:add']">新增</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['product:product:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改商品管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="productRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品图片" prop="productImage">
          <image-upload v-model="form.productImage"/>
        </el-form-item>
        <el-form-item label="商品排序" prop="productSort">
          <el-input v-model="form.productSort" placeholder="请输入商品排序" />
        </el-form-item>
        <el-form-item label="销售价格" prop="salePrice">
          <el-input v-model="form.salePrice" placeholder="请输入销售价格" />
        </el-form-item>
        <el-form-item label="进货价格" prop="purchasePrice">
          <el-input v-model="form.purchasePrice" placeholder="请输入进货价格" />
        </el-form-item>
        <el-form-item label="商品介绍" prop="productDesc">
          <el-input v-model="form.productDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="库存量" prop="stock">
          <el-input v-model="form.stock" placeholder="请输入库存量" />
        </el-form-item>
        <el-form-item label="销量" prop="salesVolume">
          <el-input v-model="form.salesVolume" placeholder="请输入销量" />
        </el-form-item>
        <el-form-item label="商品类目ID" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入商品类目ID" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Product">
import { listProduct, getProduct, delProduct, addProduct, updateProduct } from "@/api/product/product"

const { proxy } = getCurrentInstance()
const { pro_status } = proxy.useDict('pro_status')

const productList = ref([])
const productOptions = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const title = ref("")
const isExpandAll = ref(true)
const refreshTable = ref(true)

const data = reactive({
  form: {},
  queryParams: {
    productId: null,
    productName: null,
    productStatus: null,
  },
  rules: {
    productName: [
      { required: true, message: "商品名称不能为空", trigger: "blur" }
    ],
    salePrice: [
      { required: true, message: "销售价格不能为空", trigger: "blur" }
    ],
    categoryId: [
      { required: true, message: "商品类目ID不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询商品管理列表 */
function getList() {
  loading.value = true
  listProduct(queryParams.value).then(response => {
    productList.value = proxy.handleTree(response.data, "categoryId", "productId")
    loading.value = false
  })
}

/** 查询商品管理下拉树结构 */
function getTreeselect() {
  listProduct().then(response => {
    productOptions.value = []
    const data = { categoryId: 0, productName: '顶级节点', children: [] }
    data.children = proxy.handleTree(response.data, "categoryId", "productId")
    productOptions.value.push(data)
  })
}
	
// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    productId: null,
    productName: null,
    productStatus: null,
    productImage: null,
    productSort: null,
    salePrice: null,
    purchasePrice: null,
    productDesc: null,
    stock: null,
    salesVolume: null,
    categoryId: null,
    createTime: null,
    createBy: null,
    updateBy: null,
    remark: null
  }
  proxy.resetForm("productRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset()
  getTreeselect()
  if (row != null && row.categoryId) {
    form.value.productId = row.categoryId
  } else {
    form.value.productId = 0
  }
  open.value = true
  title.value = "添加商品管理"
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false
  isExpandAll.value = !isExpandAll.value
  nextTick(() => {
    refreshTable.value = true
  })
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  reset()
  await getTreeselect()
  if (row != null) {
    form.value.productId = row.productId
  }
  getProduct(row.productId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改商品管理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["productRef"].validate(valid => {
    if (valid) {
      if (form.value.productId != null) {
        updateProduct(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addProduct(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除商品管理编号为"' + row.productId + '"的数据项？').then(function() {
    return delProduct(row.productId)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

getList()
</script>
